<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="合同id" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          placeholder="请输入合同id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="开始时间" prop="beginDate">
        <el-date-picker clearable v-model="queryParams.beginDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间"> </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间"> </el-date-picker>
      </el-form-item> -->
      <el-form-item label="支付比例" prop="kgyfkbl">
        <el-input v-model="queryParams.kgyfkbl" placeholder="请输入开工预付款比例" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付条款" prop="zftk">
        <el-input v-model="queryParams.zftk" placeholder="请输入支付条款" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8 cd-pr8 top-right-btn small-line-height">
        <el-col :span="1.5" v-if="showButton">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['cm:bsContractInfoMilestone:add']" :disabled="disabled">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['cm:bsContractInfoMilestone:export']">导出</el-button>
        </el-col> -->
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>
    </el-form>
    <el-table border stripe v-loading="loading" :data="bsContractInfoMilestoneList"
      @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="支付比例(%)" align="center" prop="kgyfkbl" />
      <el-table-column label="支付条款" align="center" prop="zftk" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column v-if="showButton" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cm:bsContractInfoMilestone:edit']" :disabled="disabled">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cm:bsContractInfoMilestone:remove']" :disabled="disabled">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改合同信息里程碑对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close="cancel" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="合同名称" prop="contractId">
          <!-- <el-input v-model="form.contractId" placeholder="" /> -->
          <el-select v-model="form.contractId" disabled placeholder="请选择合同名称" style="width: 100%;">
            <el-option v-for="item in bsContractInfoList" :key="item.contractId" :label="item.contractName"
              :value="item.contractId"> </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="开始时间" prop="beginDate">
          <el-date-picker clearable v-model="form.beginDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开始时间"> </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker clearable v-model="form.endDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择结束时间"> </el-date-picker>
        </el-form-item> -->
        <el-form-item label="支付比例" prop="kgyfkbl">
          <!-- <el-input v-model="form.kgyfkbl" placeholder="请输入支付比例" /> -->
          <el-input-number class="input-number" v-model="form.kgyfkbl" placeholder="请输入支付比例" :precision="2" :min="0"
            :max="100" :controls="false" style="width: 100%;text-align: left;"></el-input-number>
        </el-form-item>
        <el-form-item label="支付条款" prop="zftk">
          <el-input v-model="form.zftk" placeholder="请输入支付条款" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBsContractInfo } from "@/api/cm/bsContractInfo";

import { listBsContractInfoMilestone, getBsContractInfoMilestone, delBsContractInfoMilestone, addBsContractInfoMilestone, updateBsContractInfoMilestone } from "@/api/cm/bsContractInfoMilestone";

export default {
  name: "BsContractInfoMilestone",
  props: {
    disabled: {
      type: Boolean,
      default: () => false
    },
    showButton: {
      type: Boolean,
      default: () => true
    },
    contractId: {
      type: String,
      default: () => ''
    }
  },
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同信息里程碑表格数据
      bsContractInfoMilestoneList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractId: undefined,
        beginDate: undefined,
        endDate: undefined,
        kgyfkbl: undefined,
        zftk: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractId: [{ required: true, message: "合同名称不能为空", trigger: "blur" }]
      },
      bsContractInfoList: []
    };
  },
  watch: {
    contractId: {
      handler (newVal) {
        this.queryParams.contractId = newVal;
        this.getList();
      },
      deep: true
    }
  },
  created () {
    // this.getList();
  },
  methods: {
    /** 查询合同管理列表 */
    getHtgllb () {
      this.loading = true;
      listBsContractInfo({
        pageNum: 1,
        pageSize: 1000
      }).then((response) => {
        this.bsContractInfoList = response.rows;
      });
    },
    /** 查询合同信息里程碑列表 */
    getList () {
      if (!this.queryParams.contractId) {
        return;
      };
      this.loading = true;
      listBsContractInfoMilestone(this.queryParams).then((response) => {
        this.bsContractInfoMilestoneList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      console.log(11111111111);

      this.open = false;
      this.loading = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        contractId: this.queryParams.contractId,
        beginDate: undefined,
        endDate: undefined,
        kgyfkbl: undefined,
        remark: undefined,
        zftk: undefined,
        createBy: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      // if (!this.$route.query.contractId) {
      //   this.$modal.msgError(`请现在`);
      //   return;
      // }
      this.reset();
      this.getHtgllb();
      this.open = true;
      this.title = "添加合同信息里程碑";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      this.getHtgllb();
      const id = row.id || this.ids;
      getBsContractInfoMilestone(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改合同信息里程碑";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBsContractInfoMilestone(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsContractInfoMilestone(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除合同信息里程碑编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBsContractInfoMilestone(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "cm/bsContractInfoMilestone/export",
        {
          ...this.queryParams
        },
        `bsContractInfoMilestone_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped>
.input-number {
  ::v-deep .el-input__inner {
    text-align: left;
  }
}
</style>
